package top.haijunit.work.controller.system;

import com.alibaba.excel.EasyExcelFactory;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import top.haijunit.work.core.doamin.PageQuery;
import top.haijunit.work.core.doamin.Result;
import top.haijunit.work.core.doamin.ResultPage;
import top.haijunit.work.modules.access.domain.RoleInfo;
import top.haijunit.work.modules.access.service.RoleService;

import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Collection;

/**
 * 【说明类功能】
 *
 * @author zhanghaijun
 * @since 2024-08-30
 */
@Slf4j
@RestController
@RequestMapping("/access/role")
@Tag(name = "角色管理", description = "角色管理")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class AccessRoleController {

    private final RoleService roleService;

    @ApiOperationSupport(order = 1)
    @Operation(summary = "分页查询")
    @GetMapping("/page")
    public ResultPage<RoleInfo> listPage(PageQuery pageQuery, RoleInfo dto) {
        return roleService.selectPageList(pageQuery, dto);
    }

    @ApiOperationSupport(order = 2)
    @Operation(summary = "列表查询")
    @GetMapping("/list")
    public Result<Collection<RoleInfo>> list(RoleInfo dto) {
        return Result.success(roleService.selectList(dto));
    }

    @ApiOperationSupport(order = 4)
    @Operation(summary = "数据导出")
    @GetMapping("/export")
    public void export(RoleInfo dto, HttpServletResponse response) throws IOException {
        // 设置响应头
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/vnd.ms-excel");
        response.setHeader("Content-Disposition", "attachment;filename*=utf-8''" + URLEncoder.encode("角色信息", StandardCharsets.UTF_8) + ".xlsx");
        // 创建 Cell 居中样式
        WriteCellStyle writeCellStyle = new WriteCellStyle();
        writeCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        writeCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
        // 数据写入
        EasyExcelFactory.write(response.getOutputStream(), RoleInfo.class)
            .sheet("角色信息")
            .registerWriteHandler(new HorizontalCellStyleStrategy(null, writeCellStyle))
            .doWrite(roleService.selectList(dto));
    }

    @ApiOperationSupport(order = 5)
    @Operation(summary = "详情查询")
    @GetMapping("/detail/{code}")
    public Result<RoleInfo> detail(@PathVariable("code") Long code) {
        return Result.success(roleService.selectDetail(code));
    }

    @ApiOperationSupport(order = 6)
    @Operation(summary = "新增")
    @PostMapping("/add")
    public Result<Long> add(@RequestBody @Validated RoleInfo positionInfo) {
        Long code = roleService.insert(positionInfo);
        return Result.success(code);
    }

    @ApiOperationSupport(order = 7)
    @Operation(summary = "修改")
    @PutMapping("/edit")
    public Result<Long> update(@RequestBody @Validated RoleInfo positionInfo) {
        Long code = roleService.modify(positionInfo);
        return Result.success(code);
    }

    @ApiOperationSupport(order = 8)
    @Operation(summary = "删除")
    @DeleteMapping("/delete/{code}")
    public Result<Long> delete(@PathVariable Long code) {
        return Result.success(roleService.delete(code));
    }
}
